// 入口函数
$(function () {
    // 需求1： 初始化文章分类列表
    // 封装
    let layer = layui.layer
    initArtCateList()
    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates',
        }).then(res => {
            // console.log(res.data);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            let arr = []

            res.data.data.forEach(ele => {
                arr.push(`
                    <tr>
                        <td>${ele.Id}</td>
                        <td>${ele.name}</td>
                        <td>${ele.alias}</td>
                        <td>
                            <button data-id='${ele.Id}' class=" btn-edit layui-btn layui-btn-xs">修改</button>
                            <button data-id='${ele.Id}' class="btn-del layui-btn layui-btn-xs layui-btn-danger">删除</button>
                        </td>
                    </tr>
                `)
            });

            $('tbody').empty().html(arr.join(''))
        })

    }

    // 需求2
    let indexAdd = 0
    $('#addBtn').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            area: ['500px', '251px'],
            title: '添加文章分类',
            content: `
                <form id='addForm' class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            `
        });

    })

    // 需求3 添加文章分类 后添加的动态元素，需要使用事件委托
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return localStorage.msg(res.data.message)
            }
            layer.msg('恭喜您，添加文章类别成功')

            initArtCateList()
            $('#addForm')[0].reset()
            // 关闭对话框
            layer.close(indexAdd)

        })
    })


    // 需求4： 修改分类弹出层
    let indexEdit = 0
    // let layer = layui.layer
    $('tbody').on('click', '.btn-edit', function () {
        // console.log(111);
        indexEdit = layer.open({
            type: 1,
            area: ['500px', '251px'],
            title: '修改文章分类',
            content: `
                <form id='editForm' class="layui-form" lay-filter='editForm' action="">

                    <input type="hidden" name="Id">


                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            `
        })

        let id = $(this).attr('data-id')
        axios({
            method: 'GET',
            url: '/my/article/cates/' + id,
        }).then(res => {
            // console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            console.log(res.data.data);
            layui.form.val('editForm', res.data.data)
        })
    })


    // 需求5：
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()
        // 修改分类
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return localStorage.msg(res.data.message)
            }
            layer.msg('恭喜您，修改文章类别成功')

            initArtCateList()
            $('#editForm')[0].reset()
            // 关闭对话框
            layer.close(indexEdit)

        })
    })

    // 需求6: 删除文章分类
    $('tbody').on('click', '.btn-del', function () {
        // 获取id
        let id = $(this).attr('data-id')
        console.log(id);
        layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
            //do something
            axios({
                method: 'GET',
                url: '/my/article/deletecate/' + id
            }).then(res => {
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message)
                }
                layer.msg('恭喜您，删除文章类别成功')
                initArtCateList()
            })
            layer.close(index);
        });


    })


})